package com.etc.controller;

import com.etc.entity.User;
import com.etc.entity.Users;
import com.etc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

// 用户类
@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired // 注入用户逻辑类
    UserService userService;
    @RequestMapping("/queryAllUsers")
    // 查询所有的用户
    public ModelAndView queryAllUsers(){
      List<User> list=  userService.queryAllUsers();
        ModelAndView mv  = new ModelAndView();
        mv.addObject("list",list);
        mv.setViewName("index");
    return mv;
    }
    
    // 返回主页
    @RequestMapping("/queryUsers1")
    public ModelAndView querysUsers1(){
    	ModelAndView mv  = new ModelAndView();
        mv.setViewName("index2");
    return mv;
    }
    
    // 查询该页码下的用户
    @RequestMapping("/queryUsers2/{page}")
    public ModelAndView querysUsers2(@PathVariable("page") int page){
      List<Users> list=  userService.queryUsers(page); // 查询该页码下的用户集合
      
        ModelAndView mv  = new ModelAndView();
        mv.addObject("list",list);// 放入model中
        int totalCount = userService.getAll();// 获取总数
        int pageCount = totalCount / 5;// 获取总页数
        if ( totalCount % 5 != 0) { //不能整除5
        	pageCount++;//页码+1
        }
        
        List<Integer> pageList = new ArrayList<Integer>(); // 获取页码集合
        for (int i = 0 ;i < pageCount; i++) {
        	pageList.add(i);
        }
        // 存变量
        mv.addObject("pageList", pageList);
        mv.addObject("totalCount", userService.getAll());
        
        mv.setViewName("User_Manage");
        
    return mv;
    }
    // 根据id获取User类
    @RequestMapping("/queryUsers3/{id}")
    public ModelAndView querysUsers3(@PathVariable("id") int id){
      Users u=  userService.queryUsers3(id);
     
        ModelAndView mv  = new ModelAndView();
        mv.addObject("Users",u);
        
        mv.setViewName("User_Edit");
        
    return mv;
    }
    
    // 更新update
    @RequestMapping("/update")
    public ModelAndView updateUser(Users u){
    	userService.updateUsers(u);
     
     
    	ModelAndView mv  = new ModelAndView();
        mv.setViewName("index2");
           
    return mv;
    }
    
    // 根据昵称和页码获取User集合
    @RequestMapping("/queryUsersFilter")
    public ModelAndView queryUsersFilter(int page, String nickname){
    	System.out.println(page);
      List<Users> list=  userService.queryUsersFilter(page, nickname);
     
        ModelAndView mv  = new ModelAndView();
        mv.addObject("list",list);
        int totalCount = userService.queryAllUsersFilter(nickname);
        int pageCount = totalCount / 5;
        if ( totalCount % 5 != 0) {
        	pageCount++;
        }
        
        List<Integer> pageList = new ArrayList<Integer>();
        for (int i = 0 ;i < pageCount; i++) {
        	pageList.add(i);
        }
        mv.addObject("pageList", pageList);
        mv.addObject("totalCount", userService.getAll());
        
        mv.setViewName("User_Manage");
        
    return mv;
    }
    
    // 删除指定id的User
    @RequestMapping("/delete/{id}")
    public ModelAndView deleteUser(@PathVariable("id") int id){
    	userService.deleteUsers(id);
    	ModelAndView mv  = new ModelAndView();
        mv.setViewName("index2");
           
    return mv;
    }
    
 // 是否存在指定身份证号的User
    @RequestMapping(value="/exist/{idcard}" )
    @ResponseBody
    public String exist(@PathVariable("idcard") String idcard){
    	if(userService.existCard(idcard))
    		return "error";
    	return "success";
    }
}
